import { dateFormat } from '@/utils/date'
import { request } from '@/utils/request'

export default {
  // 获取所有项目信息列表
  async getProjectList ({ commit }) {
    const list = await request('/project/list')
    if (list.success) {
      commit('setProjectList', list.data)
    }
  },

  // 设置当前选择项目信息
  settleSelectedProject ({ commit }, projectId) {
    commit('setSelectedProject', projectId)
  },

  // 获取所有操作员信息
  async getOperators ({ commit }) {
    const list = await request('/operator/list')
    if (list.success) {
      commit('setOperators', list.data)
    }
  },

  // 设置操作员信息
  settleOperator ({ commit }, operator) {
    commit('setSelectedOperator', operator)
  },

  // 获取数据记录列表
  async getRecords ({ commit }, params) {
    const { pageNo, pageSize, projectId, workName } = params
    const url = `/summary/list?projectId=${projectId}&workName=${workName}&pageNo=${pageNo}&pageSize=${pageSize}`
    const res = await request(url)
    if (res.success) {
      const list = res.data.records.map(item => {
        item.date = dateFormat(item.createdAt, 'Y-m-d H:i:s')
        item.startTime = dateFormat(item.startTime, 'Y-m-d H:i:s')
        item.endTime = dateFormat(item.endTime, 'Y-m-d H:i:s')
        const diff = item.timeLength / 1000
        item.timeLength = `${parseInt(diff / 60)}:${diff % 60}`
        if (item.deepLength) {
          item.deepLength = item.deepLength.toFixed(2)
        }
        return item
      })
      commit('setRecords', { list, total: res.data.total })
    }
  },

  // 创建项目信息
  async createProject ({ commit, dispatch }, options) {
    const url = '/project/create-project'
    const res = await request(url, 'POST', {}, options)
    if (res.success) {
      dispatch('getProjectList')
    }
    return res
  },

  // 创建操作员信息
  async createOperator ({ commit, dispatch }, options) {
    const { name, gender, workId } = options
    const url = `/operator/create-operator?name=${name}&gender=${gender}&workId=${workId}`
    const res = await request(url)
    if (res.success) {
      dispatch('getOperators')
    }

    return res
  },

  // 更新项目信息
  async updateProject ({ commit }, options) {
    const url = '/project/update-project'
    const res = await request(url, 'POST', {}, options)
    return res
  },

  // 删除项目信息
  async deleteProject ({ commit }, option) {
    const { projectId } = option
    const url = `/project/delete-project?projectId=${projectId}`
    const res = await request(url)
    return res
  },

  // 设置工作模式
  setWorkingMode ({ commit }, mode) {
    commit('setMode', mode)
  },

  // 上传数据
  async uploadData ({ commit }, data) {
    const url = '/summary/record-data'
    const res = await request(url, 'POST', {}, data)
    return res
  },

  // 设置记录状态
  getRecordState ({ commit }, recording) {
    commit('setRecordState', recording)
  },

  // 设置当前实时数据
  getTimingData ({ commit }, data) {
    console.log('timing data', data)
    commit('setTimingData', data)
  },

  // 获取数据分析
  async getChartData ({ commit }, options) {
    const url = '/statistic/chart-data'
    const res = await request(url, 'GET', options)
    return res
  },

  // 获取导向数据
  async getLeaderTrackData ({ commit }, options) {
    const url = '/record/track'
    const res = await request(url, 'GET', options)
    return res
  },

  // 设置当前钻杆数
  async getCLNumber ({ commit }, num) {
    commit('setCLNumber', num)
  },

  // 上传数据
  async uploadOrientationData ({ commit }, data) {
    const url = '/record/orientation-data'
    const res = await request(url, 'POST', {}, { data })
    return res
  },

  // 更新数据
  async updateRecord ({ commit }, data) {
    const url = '/summary/update-data'
    const res = await request(url, 'POST', {}, data)
    return res
  },

  // 文件下载
  async downloadData ({ commit }, options) {
    // const { projectId, workName } = options
    // const url = `/statistic/excel?projectId=${projectId}&workName=${workName}`
    const url = '/statistic/excel'
    const res = await request(url, 'GET', options, {}, true)
    return res
  }

  // 删除记录信息
  // async deleteRecord ({ commit }, options) {
  //   return new Promise((resolve, reject) => {
  //     const url = '/summary/delete'
  //     const res = await request(url, 'GET', options).catch(err => reject(err))
  //     resolve(res)
  //   })
  // }
}
